﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Ongoal.Quotation.Common;
using Ongoal.Quotation.Http;
using Ongoal.Quotation.Model;
using Ongoal.Quotation.Redis;
using Ongoal.Quotation.Sqlsugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.Service
{
    /// <summary>
    /// 服务类基类
    /// </summary>
    public class ServiceBase : IServiceBase, ICommonBase
    {
        private IConfiguration? _configuration;
        private ILogger<ServiceBase>? _logger;
        private IDbFactory? _dbFactory;
        private UserInfo? _user;
        private IRedisProvider? _redis;
        private IMircoServiceClient? _service;

        /// <summary>
        /// 获取对应的服务实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T? GetService<T>()
        {
            var temp = ServiceProviderContext.ServiceProvider!.GetService(typeof(T));
            if (temp == null)
            {
                return default(T);
            }
            return (T)temp;
        }


        /// <summary>
        /// 数据库
        /// </summary>
        public IDbFactory? Db
        {
            get
            {
                if (_dbFactory == null)
                    _dbFactory = ServiceProviderContext.ServiceProvider.GetService<IDbFactory>();
                return _dbFactory;
            }
        }

        /// <summary>
        /// 配置
        /// </summary>
        public IConfiguration? Config
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = ServiceProviderContext.ServiceProvider.GetService<IConfiguration>();
                }
                return _configuration;
            }
        }

        /// <summary>
        /// 日志
        /// </summary>
        public ILogger<ServiceBase>? Logger
        {
            get
            {
                if (_logger == null)
                {
                    _logger = ServiceProviderContext.ServiceProvider.GetService<ILogger<ServiceBase>>();
                }
                return _logger;
            }
        }


        /// <summary>
        /// 日志
        /// </summary>
        public UserInfo? CurrentUser
        {
            get
            {
                if (_user == null)
                {
                    var httpContext = ServiceProviderContext.HttpContextAccessor.HttpContext;
                    var allClaims = from c in httpContext!.Request.HttpContext.User.Claims select new { c.Type, c.Value };
                    if (allClaims != null)
                    {
                        var loguser = allClaims.FirstOrDefault(x => x.Type == "loginuser");
                        if (loguser != null)
                        {
                            string loginfo = Des.Decrypt(loguser.Value, Des.DefaultKEY);    
                            _user = JsonConvert.DeserializeObject<UserInfo>(loginfo);
                        }
                    }
                }
                return _user;
            }
        }

        /// <summary>
        /// Redis
        /// </summary>
        public IRedisProvider? Redis
        {
            get
            {
                if (_redis == null)
                {

                    _redis = ServiceProviderContext.ServiceProvider.GetService<IRedisProvider>();
                }
                return _redis;
            }
        }


        /// <summary>
        /// 微服务通讯实例
        /// </summary>
        public IMircoServiceClient? Service
        {
            get
            {
                if (_service == null)
                {

                    _service = ServiceProviderContext.ServiceProvider.GetService<IMircoServiceClient>();
                }
                return _service;
            }
        }

    }
}
